package com.xushi.controller;

import com.xushi.model.User;
import com.xushi.model.VeriCode;
import com.xushi.service.UserService;
import com.xushi.service.VeriCodeService;
import com.xushi.utils.SmsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Controller
public class HomeController {
    @Autowired
    UserService userService;

    @Autowired
    private VeriCodeService veriCodeService;

    @GetMapping("/loginRedirect")
    @ResponseBody
    public Map<String, String> login(HttpServletResponse response) {
        response.setStatus(HttpStatus.FORBIDDEN.value());
        Map<String, String> map = new HashMap<>();
        map.put("url", "/login");
        return map;
    }

    @RequestMapping("/loginSuccess")
    @ResponseBody
    public Map<String, Object> loginSuccess() {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 1);
        map.put("message", "login successfully!");
        return map;
    }

    @PostMapping("/logoutSuccess")
    @ResponseBody
    public Map<String, Object> logoutSuccess() {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 1);
        map.put("message", "logout successfully!");
        return map;
    }

    @RequestMapping("/loginFail")
    @ResponseBody
    public Map<String, Object> loginFail(HttpServletResponse response) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", "2");
        map.put("message", "login failure...");
        return map;
    }

    // used for web test
    @GetMapping("/webLogin")
    public String testLogin(HttpServletResponse response) {
        response.setStatus(HttpStatus.FORBIDDEN.value());
        Map<String, String> map = new HashMap<>();
        map.put("url", "/login");
        return "login";
    }

    @PostMapping("/register")
    @ResponseBody
    public Map<String, Object> register(String username, String password, String code) {
        Map<String, Object> map = new HashMap<>();
        if (username == null || password == null || code == null) {
            map.put("code", 3);
            return map;
        }
        VeriCode veriCode = veriCodeService.getVeriCodeByPhoneNumber(username);
        if (!veriCode.getCode().equals(code)) {
            map.put("code", 4);
            return map;
        }
        try {
            userService.createUser(username, password);
            map.put("code", 1);
            map.put("message", "创建成功");
        } catch (Exception e) {
            System.out.println(e);
            map.put("code", 2);
            map.put("message", "创建失败");
        }
        return map;
    }

    @PostMapping("/resetPassword")
    @ResponseBody
    public Map<String, Object> resetPassword(String username, String password, String code) {
        Map<String, Object> map = new HashMap<>();
        if (username == null || password == null || code == null) {
            map.put("code", 3);
            return map;
        }
        VeriCode veriCode = veriCodeService.getVeriCodeByPhoneNumber(username);
        if (!veriCode.getCode().equals(code)) {
            map.put("code", 4);
            return map;
        }
        User user = userService.findBySsoId(username);
        if (user == null) {
            map.put("code", 3);
            map.put("message", "用户不存在");
            return map;
        }
        try {
            user.setPassword(password);
            userService.saveOrUpdateUser(user);
            map.put("code", 1);
            map.put("message", "创建成功");
        } catch (Exception e) {
            System.out.println(e);
            map.put("code", 2);
            map.put("message", "创建失败");
        }
        return map;
    }



    @GetMapping("/register")
    public String register() {
        return "register";
    }

    @RequestMapping("/access_denied")
    @ResponseBody
    public Map<String, String> access_denied() {
        Map<String, String> map = new HashMap<>();
        map.put("reason", "forbidden");
        return map;
    }

    @RequestMapping("/sendSmsTest")
    @ResponseBody
    public Map<String, Object> sendSmsTest(@RequestParam String phoneNumber) {
        String verificationCode = SmsUtil.generateVerificationCode();
        return SmsUtil.sendSms(phoneNumber, verificationCode);
    }

    @RequestMapping("/sendSms")
    @ResponseBody
    public Map<String, Object> sendSms(@RequestParam String phoneNumber) {
        String verificationCode = SmsUtil.generateVerificationCode();
        veriCodeService.addVeriCode(phoneNumber, verificationCode);
        return SmsUtil.sendSms(phoneNumber, verificationCode);
    }
}
